//笔试强训：牛牛冲钻五

#include <iostream>
using namespace std;

int main() {
    int T = 0, n = 0, k = 0; 
    cin >> T;
    while(T--)
    {
        cin >> n >> k;
        int stars = 0;
        char n1 = 'L', n2 = 'L', n3 = 'L';
        while(n--)
        {
            cin >> n3;
            if(n3 == 'W')
            {
                if(n1 == 'W' && n2 == 'W')
                    stars += k;
                else
                    stars += 1;
            }
            else 
            {
                stars -= 1;
            }

            n1 = n2;
            n2 = n3;
        }
        cout << stars << endl;
    }
}